import { defineStore } from 'pinia';
import { Session } from '/@/utils/storage';
import { useLoginApi } from '/@/api/login/index';
import { toolsApi } from '/@/api/tools';
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: () => ({
		userInfos: {
			userName: '',
			photo: '',
			time: 0,
			roles: [],
			authBtnList: [],
		},
		versions: null
	}),
	actions: {
		async setUserInfos() {
			// 存储用户信息到浏览器缓存
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				const userInfos = await this.getApiUserInfo();
				this.userInfos = userInfos;
			}
		},
		//
		async getApiUserInfo() {
			return new Promise((resolve) => {
				useLoginApi().getUserInfoByToken().then(res => {
					const userInfos = res.data
					Session.set('userInfo', res.data);
					userInfos.photo = '',
					userInfos.roles =  ['common'],
					resolve(userInfos);
				}).catch(() =>{
	
				})
		
			});
		},

		async setVersion() {
			// const versions = await this.getVersionInfo();
			// this.versions = versions;
			this.versions = '';
		},

		// async getVersionInfo() {
		// 	return new Promise((resolve) => {
		// 		toolsApi().getVersion().then(res => {
		// 			resolve(res.data);
		// 		}).catch(() =>{
	
		// 		})
		// 	});
		// },
	},
});
